package com.halflife.query;

import org.apache.ibatis.jdbc.SQL;
import org.apache.ibatis.session.SqlSession;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import javax.transaction.Transactional;
import java.util.List;

/**
 * Created by zhaoyf on 2018/9/2.
 */
@Component
public class DaoSupportImpl  implements DaoSupport  {

    private final SqlSession sqlSession;

    public DaoSupportImpl(SqlSession sqlSession) {
        this.sqlSession = sqlSession;
    }
    /**
     * 插入.
     * @param queryHelper
     */
    public void insert(QueryHelper queryHelper) {
        sqlSession.insert(queryHelper.getMethod(),queryHelper.getRealParams());
    }

    /**
     * 插入.
     * @param method
     * @param param
     */
    public void insert(String method, Object param) {
        sqlSession.insert(method,param);
    }

    /**
     * 删除.
     * @param queryHelper
     */
    public void delete(QueryHelper queryHelper) {
        sqlSession.delete(queryHelper.getMethod(),queryHelper.getRealParams());
    }

    /**
     * 删除.
     * @param method
     * @param o
     */
    public void delete(String method, Object o) {
        sqlSession.delete(method,o);
    }

    /**
     * 更新.
     * @param queryHelper
     */
    public void update(QueryHelper queryHelper) {
        sqlSession.update(queryHelper.getMethod(),queryHelper.getRealParams());
    }

    /**
     * 更新.
     * @param method
     * @param o
     */
    public void update(String method, Object o) {
        sqlSession.update(method,o);
    }

    /**
     * 查找一个.
     * @param queryHelper
     * @param <T>
     * @return
     */
    public <T> T select(QueryHelper queryHelper) {
        return sqlSession.selectOne(queryHelper.getMethod(),queryHelper.getRealParams());
    }

    /**
     * 查找一个.
     * @param method
     * @param o
     * @param <T>
     * @return
     */
    public <T> T select(String method, Object o) {
        return sqlSession.selectOne(method,o);
    }

    /**
     * 查找多个.
     * @param queryHelper
     * @param <T>
     * @return
     */
    public <T> List<T> selectList(QueryHelper queryHelper) {
        return sqlSession.selectList(queryHelper.getMethod(),queryHelper.getRealParams());
    }

    /**
     * 查找多个.
     * @param method
     * @param o
     * @param <T>
     * @return
     */
    public <T> List<T> selectList(String method, Object o) {
        return sqlSession.selectList(method,o);
    }

    /**
     * 查找所有.
     * @param method
     * @param <T>
     * @return
     */
    public <T> List<T> selectWithoutParams(String method) {
        return sqlSession.selectList(method);
    }

    /**
     * 分页数据.
     * @param pageNum
     * @param pageSize
     * @param queryHelper
     * @param <T>
     * @return
     */
    public <T> List<T> getPageData(int pageNum, int pageSize, QueryHelper queryHelper) {
//        sqlSession.select();
        return null;
    }
//    public Page getPageData(int pageNum){
//        int totleRecord = daoSupport.select("Article.getTotleRecord",null);
//        Page page = new Page(pageNum, Configuration.pageSize,totleRecord);
//        QueryHelper helper = new QueryHelper();
//        helper.setNameSpace("Article")
//                .setMethodId("getPageData")
//                .setParams("startIndex",page.getStartIndex())
//                .setParams("pageSize",page.getPageSize());
//        List list = daoSupport.selectList(helper);
//        page.setList(list);
//        return page;
//    }

}
